<template>
  <div class="app-header-stick" :class="{stick: isStick}">
    <div class="container">
      <!-- 网站Logo -->
      <router-link to="/" class="xtx_logo">
        <h1>小兔鲜儿</h1>
      </router-link>
      <!-- 主导航 -->
      <app-header-nav :cateList="cateList"></app-header-nav>
      <!-- 右导航 -->
      <div class="right-nav">
        <transition :name="show?'fade-in1':'fade-in2'" mode="out-in">
          <div class="href" v-if="!show" key="1">
            <router-link to="/brand">品牌</router-link>
            <router-link to="/special">专题</router-link>
          </div>
          <div class="search" v-else key="2">
            <span @click="show=false">
              <i class="iconfont icon-angle-right"></i>
              <i class="iconfont icon-angle-right"></i>
            </span>
            <input v-model="keyword" @keyup.enter="toSearch()" type="text" placeholder="搜一搜">
          </div>
        </transition>
        <div class="icon">
          <a href="javascript:;" @click="show=true"><i class="iconfont icon-search"></i></a>
          <router-link to="/cart"><i class="iconfont icon-cart"></i></router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import AppHeaderNav from './app-header-nav'
export default {
  name: 'app-header-stick',
  components: { AppHeaderNav },
  props: ['cateList'],
  data () {
    return {
      isStick: false,
      show: false,
      keyword: ''
    }
  },
  mounted () {
    window.addEventListener('scroll', () => {
      this.isStick = document.documentElement.scrollTop >= 104
    }, false)
  },
  methods: {
    toSearch () {
      this.$router.push({ path: '/search', query: { keyword: encodeURIComponent(this.keyword) } })
      this.keyword = ''
    }
  }
}
</script>

<style scoped lang='less'>
.fade-in1 {
  &-enter {
    opacity: 0;
    transform: translate3d(0,10px,0);
    &-active {
      transition: all .2s linear;
    }
    &-to {
      opacity: 1;
    }
  }
  &-leave {
    opacity: 1;
    &-active {
      transition: all .2s linear;
    }
    &-to {
      opacity: 0;
      transform: translate3d(0,-10px,0);
    }
  }
}
.fade-in2 {
  &-enter {
    opacity: 0;
    transform: translate3d(0,-10px,0);
    &-active {
      transition: all .2s linear;
    }
    &-to {
      opacity: 1;
    }
  }
  &-leave {
    opacity: 1;
    &-active {
      transition: all .2s linear;
    }
    &-to {
      opacity: 0;
      transform: translate3d(0,10px,0);
    }
  }
}
.app-header-stick {
  width: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 999;
  background: #fff;
  border-bottom: 1px solid #e4e4e4;
  transform: translate3d(0, -100%, 0);
  opacity: 0;
  &.stick {
    z-index: 999;
    transform: translate3d(0, 0, 0);
    opacity: 1;
    transition: all 0.2s linear;
  }
  .container {
    display: flex;
    align-items: center;
    position: relative;
  }
  .xtx_logo {
    width: 200px;
    height: 80px;
    text-indent: -999px;
    background-image: url(../assets/images/logo.png);
    background-size: 150px 60px;
    background-repeat: no-repeat;
    background-position: center 5px;
  }
}
.right-nav {
  width: 230px;
  display: flex;
  font-size: 16px;
  position: relative;
  line-height: 34px;
  align-items: center;
  .href {
    width: 152px;
    display: flex;
    a {
      margin-right: 44px;
      line-height: 30px;
      border-bottom: 2px solid transparent;
      margin-top: 2px;
      &:hover {
        color:@xtxColor;
        border-color: @xtxColor;
      }
    }
  }
  .search {
    width: 152px;
    display: flex;
    height: 34px;
    position: relative;
    align-items: center;
    > span {
      letter-spacing: -6px;
      width: 20px;
      position: absolute;
      left: -24px;
      top: 0;
      height: 34px;
      &:hover {
        cursor: pointer;
        color: @xtxColor;
      }
      i {
        font-size: 12px;
      }
    }
    > input {
      width: 152px;
      border-bottom: 1px solid @xtxColor;
      border-top: 1px solid transparent;
      height: 26px;
      font-size: 14px;
    }
  }
  .icon {
    flex: 1;
    display: flex;
    > a {
      width: 39px;
      text-align: center;
      color: @xtxColor;
    }
  }
}
</style>
